﻿using UnityEngine;
using System.Collections;

namespace ZZQFrameWork
{
    public abstract class BaseUI : MonoBehaviour
    {

        #region Cache GameObject && Transform

        private GameObject go;

        public GameObject CacheGameObject
        {
            get
            {
                if (go == null) go = this.gameObject;
                return go;
            }
        }

        private Transform tr;

        public Transform CacheTransform
        {
            get
            {
                if (tr == null) tr = this.transform;
                return tr;
            }
        }

        #endregion

        #region 设置UI类型

        /// <summary>
        /// 设置UI类型
        /// </summary>
        /// <returns></returns>
        public abstract Enum_UIType GetUIType();

        #endregion

        #region UI状态以及改变状态触发事件

        /// <summary>
        /// UI状态改变事件
        /// </summary>
        public event OnUIStateChangedDe OnUIStateChangedEvent;

        /// <summary>
        /// UI状态
        /// </summary>
        private Enum_UIState state;

        public Enum_UIState State
        {
            get { return state; }
            set
            {
                if (state != value)
                {
                    Enum_UIState oldState = state;
                    state = value;
                    OnUIStateChanged(oldState, state);
                    if (OnUIStateChangedEvent != null)
                    {
                        OnUIStateChangedEvent(oldState, state);
                    }
                }
            }
        }

        #endregion

        #region 实方法

        private void Awake()
        {
            OnAwake();
            State = Enum_UIState.Init;
        }

        private void Start()
        {
            OnStart();
        }

        private void Update()
        {
            OnUpdate();
        }

        public void Open(params object[] objects)
        {
            gameObject.SetActive(true);
            State = Enum_UIState.Open;
            OnOpen();
            OnPlayMusic();
        }

        public void Close()
        {
            OnStopMusic();
            OnClose();
            State = Enum_UIState.Close;
            gameObject.SetActive(false);
        }

        #endregion

        #region 虚方法

        protected virtual void OnAwake()
        {

        }

        protected virtual void OnStart()
        {

        }

        protected virtual void OnUpdate()
        {

        }

        protected virtual void OnOpen(params object[] objects)
        {

        }

        protected virtual void OnClose()
        {

        }

        protected virtual void OnPlayMusic()
        {

        }

        protected virtual void OnStopMusic()
        {

        }

        /// <summary>
        /// UI状态改变触发方法
        /// </summary>
        /// <param name="oldState">旧状态</param>
        /// <param name="newState">新状态</param>
        protected virtual void OnUIStateChanged(Enum_UIState oldState, Enum_UIState newState)
        {

        }

        #endregion
    }
}
